#ifndef __STRING_H
#define __STRING_H
#ifndef NULL
#define NULL 0
#endif
typedef unsigned size_t;
size_t strlen(const char *str)
{
    size_t len = 0;
    while (*str != '\0')
    {
        len++;
        str++;
    }
    return len;
}

void *memchr(void *str, int c, size_t n)
{
    for (void *p = str; p < str + n; p++)
    {
        if (*(char *)p == c)
            return p;
    }
    return NULL;
}
int memcmp(const void *str1, const void *str2, size_t n)
{
    char *c1 = (char *)str1, *c2 = (char *)str2;
    for (size_t i = 0; i < n; i++)
    {
        if (*(char *)str1 != *(char *)str2)
            return *(char *)str1 - *(char *)str2;
        str1++;
        str2++;
    }
}
void *memcpy(void *dest, const void *src, size_t n)
{
    char *dest_ptr = (char *)dest;
    const char *src_ptr = (const char *)src;
    for (size_t i = 0; i < n; i++)
    {
        dest_ptr[i] = src_ptr[i];
    }
    return dest;
}
void *memmove(void *dest, const void *src, size_t n)
{
    memcpy(dest, src, n);
    return dest;
}
char *strcat(char *dest, const char *src)
{
    char *ret = dest;
    while (*dest != '\0')
        dest++;
    while ((*dest++ = *src++) != '\0')
        ;
    return ret;
}
char *strncat(char *dest, const char *src, size_t n)
{
    char *ret = dest;
    while (*dest != '\0')
        dest++;
    while (*src != '\0' && n--)
        *dest++ = *src++;
    *dest = '\0';
    return ret;
}
char *strchr(char *str, int c)
{
    for (char *p = str; *p != '\0'; p++)
    {
        if (*(char *)p == c)
            return (char *)p;
    }
    return NULL;
}
int strncmp(const char *str1, const char *str2, size_t n)
{
    for (size_t i = 0; i < n; i++)
    {
        if (*(str1 + i) != *(str2 + i))
            return *(unsigned char *)(str1 + i) - *(unsigned char *)(str2 + i);
        if (*(str1 + i) == '\0')
            return 0;
    }
    return 0;
}
int strcmp(const char *str1, const char *str2)
{
    return strncmp(str1, str2, 2147483647);
}
int strcoll(const char *str1, const char *str2)
{
    return strcmp(str1, str2);
}
char *strcpy(char *dest, const char *src)
{
    return memcpy(dest, src, strlen(src) + 1);
}
char *strncpy(char *dest, const char *src, size_t n)
{
    return memcpy(dest, src, n);
}
size_t strcspn(const char *str1, const char *str2)
{
    size_t count = 0;
    for (const char *s1 = str1; *s1 != '\0'; s1++)
    {
        const char *s2;
        for (s2 = str2; *s2 != '\0'; s2++)
        {
            if (*s1 == *s2)
            {
                count++;
                break;
            }
        }
    }
    return count;
}
char *strerror(int errnum)
{
    // 未定义行为
}
char *strpbrk(const char *str1, const char *str2)
{
    // 检索字符串 str1 中第一个匹配字符串 str2 中字符的字符，不包含空结束字符。也就是说，依次检验字符串 str1 中的字符，当被检验字符在字符串 str2 中也包含时，则停止检验，并返回该字符位置。
    for (const char *s1 = str1; *s1 != '\0'; s1++)
    {
        const char *s2;
        for (s2 = str2; *s2 != '\0'; s2++)
        {
            if (*s1 == *s2)
            {
                return (char *)s1;
            }
        }
    }
    return NULL;
}
char *strrchr(const char *str, int c)
{
    size_t len = strlen(str);
    for (size_t i = 0; i < len; i++)
    {
        if (str[i] == (char)c)
        {
            return (char *)(str + i);
        }
    }
    return NULL;
}
size_t strspn(const char *str1, const char *str2)
{
    // 检索字符串 str1 中第一个不在字符串 str2 中出现的字符下标。
    size_t count = 0;
    for (const char *s1 = str1; *s1 != '\0'; s1++)
    {
        const char *s2;
        for (s2 = str2; *s2 != '\0'; s2++)
        {
            if (*s1 == *s2)
            {
                break;
            }
        }
        if (*s2 == '\0')
        {
            count++;
        }
    }
    return count;
}
char *strstr(const char *haystack, const char *needle)
{
    // 在字符串 haystack 中查找第一次出现字符串 needle 的位置。
    size_t len = strlen(needle);
    for (const char *s = haystack; *s != '\0'; s++)
    {
        if (strncmp(s, needle, len) == 0)
        {
            return (char *)s;
        }
    }
    return NULL;
}
char *strtok(char *str, const char *delim)
{
    static char *next = NULL;
    if (str == NULL)
    {
        str = next;
    }
    if (str == NULL)
    {
        return NULL;
    }
    const size_t len = strlen(str);
    for (size_t i = 0; i <= len; i++)
    {
        if (i == len || str[i] == *delim)
        {
            str[i] = '\0';
            next = str + i + 1;
            return str;
        }
    }
}
size_t strxfrm(char *dest, const char *src, size_t n)
{
    // 将字符串 src 转换为 dest，最多转换 n 个字符。
    size_t count = 0;
    for (const char *s = src; *s != '\0' && count < n; s++)
    {
        *dest++ = *s;
        count++;
    }
    *dest = '\0';
    return count;
}
#endif